import moment from 'moment'
// 闰年
const leapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// 平年
const commonYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// 月份
const month = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];


Component({
  mixins: [],
  data: {},
  props: {
    clockDay: [], // 判断打卡的天数 
    currentTime: null // 当前时间
  },
  didMount() {
    this.setCalendar();
    this.init()
  },
  didUpdate(prevProps) {
    if (prevProps.clockDay.length != this.props.clockDay.length) {
      this.init();
    }
  },
  didUnmount() { },
  methods: {
    // 设置日历
    setCalendar() {
      // 当前月份
      const currentTime = this.props.currentTime ? new Date(parseInt(this.props.currentTime)) : new Date(); // 当前时间


      let currentYear = currentTime.getFullYear();
      let currentMonth = currentTime.getMonth() + 1;
      let currentfirstWeek = this.getDay(currentMonth, currentYear, 1);
      let currentMonthDay = this.getMonthLength(currentMonth - 1, currentTime.getFullYear());

      // 上个月份 / 有多少天
      let preMonth = currentMonth - 1 <= 0 ? 12 : currentMonth - 1;
      let preMonthDay = this.getMonthLength(currentMonth - 2 < 0 ? 11 : currentMonth - 2, currentMonth - 2 < 0 ? currentTime.getFullYear() - 1 : currentTime.getFullYear());
      // 上个月是 几几年 如果当前是 2021-1 则 上一年为 2020
      let preMonthIsYear = currentMonth - 2 < 0 ? currentTime.getFullYear() - 1 : currentTime.getFullYear();

      // 下个月份 / 有多少天
      let nextMonth = currentMonth + 1 >= 12 ? 1 : currentMonth + 1;
      let nextMonthDay = this.getMonthLength(currentMonth == 12 ? 0 : currentMonth, currentMonth == 12 ? currentTime.getFullYear() + 1 : currentTime.getFullYear());
      // 下个月是 几几年 如果当前是 2021-12 则 下一年为 2021
      let nextMonthIsYear = currentMonth == 12 ? currentTime.getFullYear() + 1 : currentTime.getFullYear()

      // 生成日历
      let arrPrev = Array.from(new Array(preMonthDay + 1).keys()).slice(
        preMonthDay - currentfirstWeek + 1
      );
      arrPrev = arrPrev.map(v => ({ date: v, flag: false, timestamp: new Date(`${month[preMonth]} ${v}, ${preMonthIsYear} 00:00:00`).getTime() }))

      // 跳过上个月的日期签到--拿到上个月的数组长度跳过
      let arrCurr = Array.from(new Array(currentMonthDay + 1).keys()).slice(1);
      arrCurr = arrCurr.map(v => ({ date: v, flag: false, timestamp: new Date(`${month[currentMonth]} ${v}, ${currentYear} 00:00:00`).getTime() }))

      let calendar = [...arrPrev, ...arrCurr];
      let arrNext = Array.from(new Array(nextMonthDay + 1).keys()).slice(
        1,
        43 - calendar.length
      );
      arrNext = arrNext.map(v => ({ date: v, flag: false, timestamp: new Date(`${month[nextMonth]} ${v}, ${nextMonthIsYear} 00:00:00`).getTime() }))

      calendar = [...calendar, ...arrNext];

      this.skipPev = arrPrev.length;
      this.skipCur = arrCurr.length + this.skipPev;

      // console.log(
      //   "这个月第一天星期几：" + currentfirstWeek,
      //   "今年：" + currentYear,
      //   "这个月：" + currentMonth,
      //   "上个月：" + preMonth,
      //   "下个月：" + nextMonth,
      //   "这个月有多少天：" + currentMonthDay,
      //   "上个月有多少天：" + preMonthDay,
      //   "下个月有多少天：" + nextMonthDay,
      //   "日历", calendar
      // );

      this.setData({
        currentMonth,
        currentYear,
        calendar,
        currentTime
      });
    },
    // 获取某月第某天是星期几
    getDay(month, year, index) {
      // 月份要减去1  js 0-11
      return new Date(year, month - 1, index).getDay();
    },
    // 获取某月有几天
    getMonthLength(month, year) {
      if (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)) {
        return leapYear[month];
      } else {
        return commonYear[month];
      }
    },
    init() {
      let calendar = this.data.calendar.map((v, i) => {
        for (let j = 0; j < this.props.clockDay.length; j++) {
          if (moment(parseInt(v.timestamp)).isSame(parseInt(this.props.clockDay[j]), 'day')) {
            return { ...v, flag: true }
          }
        }
        return v
      })

      this.setData({
        calendar
      })
    },
    clockInFn(e) {
      const { timestamp } = e.currentTarget.dataset
      let nowTime = new Date().getTime()

      if (moment(parseInt(nowTime)).isSame(parseInt(timestamp), 'day')) {
        console.log("俺被触发了");
        this.props.onSetClockDayFn(nowTime)
      } else {
        console.log("时间不是时候");
      }
    }
  },
});
